package com.psa.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.psa.system.R;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.dialog.SToast;
import com.wxw.BaseLibrary.tool.L;

/**
 *
 * @author david
 * @date 2017-02-14
 */
public class UploadFileDialog {
    private Activity parentActivity;
    private Dialog dialog;
    private View layout;
    private TextView textView,tv_value;
    private ProgressBar pb_progressbar;
    private String title = "";
    private int max = 100;
    private int progress = 0;

    private Runnable changeHandler = new Runnable() {
        @Override
        public void run() {
            tv_value.setText(progress+"/"+max);
        }
    };

    public UploadFileDialog(Activity activity) {
        parentActivity = activity;
        layout = parentActivity.getLayoutInflater().inflate(R.layout.progress_dialog_layout, null);
        textView = (TextView)layout.findViewById(R.id.textView);
        tv_value = (TextView)layout.findViewById(R.id.tv_value);
        pb_progressbar = (ProgressBar)layout.findViewById(R.id.pb_progressbar);
        pb_progressbar.setMax(max);
        pb_progressbar.setProgress(0);
        tv_value.setText("0/"+max);
    }

    public void setTitle(String title){
        this.title = title;
    }

    public void setMax(int max) {
        this.max = max;
        pb_progressbar.setMax(max);
        tv_value.setText("0/"+max);
    }

    public void start(String s) {
        textView.setText(s);
        if(dialog ==null) {
            dialog = new Dialog(parentActivity);
        }
        dialog.setTitle(title);
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    public void progress(int progress) {
        this.progress=progress;
        pb_progressbar.setProgress(progress);
        parentActivity.runOnUiThread(changeHandler);
    }

    public void failure(String s) {
        pb_progressbar.setProgress(0);
        if (dialog!=null) {
            try {
                if (!(parentActivity.isDestroyed() || parentActivity.isFinishing()) && dialog.isShowing()) {
                    dialog.dismiss();
                }
            } catch (Exception e) {
                L.WriteExceptionLog("UploadFileDialog", "success", e.getMessage());
            }
        }
        if (!s.equals("")) {
            SToast.showToast(parentActivity, s, SystemStatus.Message_Show_Time);
        }
    }

    public void success(String s) {
        if (dialog!=null) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                L.WriteExceptionLog("UploadFileDialog", "success", e.getMessage());
            }
        }
        if (!s.equals("")) {
            SToast.showToast(parentActivity, s, SystemStatus.Message_Show_Time);
        }
    }
}
